package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.eqp.dto.EqpspchkParamVO;
import org.jeecg.eqp.entity.EqpSpchkCfg;
import org.jeecg.eqp.entity.EqpSpchkdetailValue;
import org.jeecg.eqp.entity.EqpSpchkheadValue;
import org.jeecg.eqp.service.IEqpSpchkCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 点检项配置
 * @Author: licy
 * @Date: 2022-10-13
 * @Version: V1.0
 */
@Api(tags = "点检项配置")
@RestController
@RequestMapping("/eqp/eqpSpchkCfg")
@Slf4j
public class EqpSpchkCfgController extends JeecgController<EqpSpchkCfg, IEqpSpchkCfgService> {
    @Autowired
    private IEqpSpchkCfgService eqpSpchkCfgService;

    /**
     * 单表分页列表查询
     *
     * @param eqpSpchkCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "点检项配置-分页列表查询")
    @ApiOperation(value = "点检项配置-分页列表查询", notes = "点检项配置-分页列表查询")
    @GetMapping(value = "/list")
    @DS("#header.his")
    public Result<IPage<EqpSpchkCfg>> queryPageList(EqpSpchkCfg eqpSpchkCfg,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<EqpSpchkCfg> queryWrapper = QueryGenerator.initQueryWrapper(eqpSpchkCfg, req.getParameterMap());
        Page<EqpSpchkCfg> page = new Page<EqpSpchkCfg>(pageNo, pageSize);
        IPage<EqpSpchkCfg> pageList = eqpSpchkCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 通过id查询
     *
     * @param equipid
     * @return
     */
    @GetMapping(value = "/queryListByEquipInfo")
    @DS("#header.his")
    public Result<?> queryListByEquipInfo(@RequestParam(name = "equipid", required = true) String equipid) {
        List<EqpSpchkCfg> list = eqpSpchkCfgService.queryListByEquipInfo(equipid);
        return Result.ok(list);
    }

    /**
     * 添加
     *
     * @param eqpSpchkCfg
     * @return
     */
    @AutoLog(value = "点检项配置-添加")
    @ApiOperation(value = "点检项配置-添加", notes = "点检项配置-添加")
    @PostMapping(value = "/add")
    @DS("#header.line")
    public Result<String> add(@RequestBody EqpspchkParamVO eqpspchkParamVO) {
        eqpSpchkCfgService.batchSpchkCfg(eqpspchkParamVO);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eqpSpchkCfg
     * @return
     */
    @AutoLog(value = "点检项配置-编辑")
    @ApiOperation(value = "点检项配置-编辑", notes = "点检项配置-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("#header.line")
    public Result<String> edit(@RequestBody EqpSpchkCfg eqpSpchkCfg) {
        //框架修改 eqpSpchkCfgService.updateById(eqpSpchkCfg);
        //return Result.OK("编辑成功!");
        return eqpSpchkCfgService.edit(eqpSpchkCfg);
    }

    /**
     * 通过map删除
     *
     * @param map
     * @return
     */
    @AutoLog(value = "点检项配置-通过id删除")
    @ApiOperation(value = "点检项配置-通过id删除", notes = "点检项配置-通过id删除")
    @RequestMapping(value = "/delete")
    @DS("#header.line")
    public Result<String> delete(@RequestBody EqpSpchkCfg eqpSpchkCfg) {
        eqpSpchkCfgService.removeById(eqpSpchkCfg);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "点检项配置-批量删除")
    @ApiOperation(value = "点检项配置-批量删除", notes = "点检项配置-批量删除")
    @RequestMapping(value = "/deleteBatch")
    @DS("#header.line")
    public Result<String> deleteBatch(@RequestBody List<EqpSpchkCfg> list) {
        list.forEach(item ->{
                eqpSpchkCfgService.removeById(item);
        });
//        this.eqpSpchkCfgService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "点检项配置-通过id查询")
    @ApiOperation(value = "点检项配置-通过id查询", notes = "点检项配置-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("#header.line")
    public Result<EqpSpchkCfg> queryById(@RequestParam(name = "id", required = true) String id) {
        EqpSpchkCfg eqpSpchkCfg = eqpSpchkCfgService.getById(id);
        if (eqpSpchkCfg == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(eqpSpchkCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param eqpSpchkCfg
     */
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, EqpSpchkCfg eqpSpchkCfg) {
        return super.exportXls(request, eqpSpchkCfg, EqpSpchkCfg.class, "点检项配置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @DS("#header.line")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EqpSpchkCfg.class);
    }

}
